package com.lfg.leetcode.leetcode0;

public class No65isNumber {

    public static void main(String[] args) {
        ;
        boolean ret = isNumber(".1");
        System.out.println(ret);
    }

    public static boolean isNumber(String s) {
        if (null == s || "".equals(s)) {
            return false;
        }
        boolean hasNum = false;
        boolean hasE = false;
        boolean hasDot = false;
        int endIndex = s.length() - 1;
        for (; endIndex >= 0; endIndex--) {
            if (s.charAt(endIndex) != ' ') {
                break;
            }
        }
        int startIndex = 0;
        for (; startIndex <= endIndex; startIndex++) {
            if (s.charAt(startIndex) != ' ') {
                break;
            }
        }
        if (startIndex > endIndex) {
            return false;
        }
        while (startIndex <= endIndex) {
            if (s.charAt(startIndex) >= '0' && s.charAt(startIndex) <= '9') {
                hasNum = true;
            } else if (s.charAt(startIndex) == 'e') {
                if (hasE || !hasNum)
                    return false;
                hasE = true;
                hasNum = false;
            } else if (s.charAt(startIndex) == '.') {
                if (hasDot || hasE)
                    return false;
                hasDot = true;
            } else if (s.charAt(startIndex) == '+' || s.charAt(startIndex) == '-') {
                if (startIndex > 1 && s.charAt(startIndex - 1) != 'e')
                    return false;
            } else {
                return false;
            }
            startIndex++;
        }
        return hasNum;
    }
}

